#!/bin/sh

# program
#
# dlopen():
# libd.so
# liba.so
# <- libb.so
#
# Expected: Undefined symbol in liba.so resolves to symbol in libd.so,
# not to symbol in libb.so.


. ./test_setup


# create libb.so
cat > libb.c << EOI
int b() { return 1; }
EOI

# build
compile_lib -o libb.so libb.c


# create liba.so
cat > liba.c << EOI
extern int b();
int a() { return b(); }
EOI

# build
compile_lib -o liba.so liba.c ./libb.so


# create libd.so
cat > libd.c << EOI
int b() { return 2; }
EOI

# build
compile_lib -o libd.so libd.c


# create program
cat > program.c << EOI
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
int
main()
{
	void* liba;
	void* libd;
	int (*a)();

	libd = dlopen("./libd.so", RTLD_NOW | RTLD_GLOBAL);
	if (libd == NULL) {
		fprintf(stderr, "Error opening libd.so: %s\n", dlerror());
		exit(117);
	}

	liba = dlopen("./liba.so", RTLD_NOW | RTLD_GLOBAL);
	if (liba == NULL) {
		fprintf(stderr, "Error opening liba.so: %s\n", dlerror());
		exit(117);
	}

	a = (int (*)())dlsym(liba, "a");
	if (a == NULL) {
		fprintf(stderr, "Error getting symbol a: %s\n", dlerror());
		exit(116);
	}

	return a();
}
EOI

 #build
compile_program_dl -o program program.c

# run
test_run_ok ./program 2
